package com.ha.garbage.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ha.garbage.R;
import com.ha.garbage.model.Garbage;

import java.util.ArrayList;
import java.util.List;

public class GarbageListAdapter extends RecyclerView.Adapter<GarbageListAdapter.MyHolder> {

    private List<Garbage> garbageList = new ArrayList<>();

    public GarbageListAdapter(List<Garbage> list){
        this.garbageList = list;

    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate((R.layout.garbage_item), null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //绑定数据
        Garbage garbage = garbageList.get(position);
        //设置数据
        holder.tv_introduction.setText(garbage.getIntroduction());
        holder.tv_kind.setText(garbage.getKind());
        holder.tv_name.setText(garbage.getName());
        String url = garbage.getImage();
        Glide.with(holder.imageView.getContext()).load(url).into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("垃圾详情")

                        .setMessage("垃圾介绍："+garbage.getIntroduction().toString())
                        .setPositiveButton("确定",null)
                        .show();
            }
        });


    }

    @Override
    public int getItemCount() {
        return garbageList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_name;
        TextView tv_introduction;
        TextView tv_kind;

        ImageView imageView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            //初始化控件
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_kind = itemView.findViewById(R.id.tv_kind);
            tv_introduction = itemView.findViewById(R.id.tv_introduction);
            imageView = itemView.findViewById(R.id.img);

        }
    }
}
